package com.ayoka.ui.launch;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.ayoka.Constant;
import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.bean.UserInfoBean;
import com.ayoka.bean.UserInformation;
import com.ayoka.databinding.ActivityVerifyMobileBinding;
import com.ayoka.mvp.VerifyMobileMVP.IVerifyMobileView;
import com.ayoka.mvp.VerifyMobileMVP.VerifyMobilePresenter;
import com.ayoka.ui.main.MainActivity;
import com.ayoka.utils.CountDownEvent;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jcp.baselibrary.utils.PatternCheck;
import com.jcp.baselibrary.utils.SPUtil;
import com.jcp.baselibrary.utils.SnackBarUtil;
import com.jcp.baselibrary.utils.TDevice;
import com.jincp.image.load.ImageLoaderProxy;

import java.util.Observable;


/**
 * =========================================================
 * 作者： 金春平
 * 类名： VerifyMobileActivity
 * 类描述：
 * 创建日期 17/03/30
 * =========================================================
 */
public class VerifyMobileActivity extends BaseActivity<ActivityVerifyMobileBinding, VerifyMobilePresenter> implements IVerifyMobileView {

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, VerifyMobileActivity.class);
        activity.startActivity(intent);
    }

    private String mobile;
    private String verifyCode;

    @Override
    public int getContentLayout() {
        return R.layout.activity_verify_mobile;
    }

    @Override
    protected void initViews() {
        mDataBinding.btnVerifyCode.setOnClickListener(this);
        mDataBinding.btnComplete.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
       setCustomToolBar(mDataBinding.customToolbar.toolbar, new ToolBarOptions());
    }

    @Override
    protected VerifyMobilePresenter getPresenter() {
        return new VerifyMobilePresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.btnVerifyCode) {
            if (mDataBinding.edtMobile.length() == 0) {
                showSnackBar(view, getString(R.string.hint_input_mobile), SnackBarUtil.TYPE_ERROR);
                return;
            }
            mobile = mDataBinding.edtMobile.getText().toString().trim();
            if (!PatternCheck.isMobile(mobile)) {
                showSnackBar(view, getString(R.string.hint_input_correct_mobile), SnackBarUtil.TYPE_WARNING);
                return;
            }
            mPresenter.sendVerifyCode(mobile);
            CountDownEvent.getInstance().addObserver(this);
            mDataBinding.btnVerifyCode.setEnabled(false);
        }
        if (view == mDataBinding.btnComplete) {
            if (mDataBinding.edtMobile.length() == 0) {
                showSnackBar(view, getString(R.string.hint_input_mobile), SnackBarUtil.TYPE_ERROR);
                return;
            }
            mobile = mDataBinding.edtMobile.getText().toString().trim();
            if (!PatternCheck.isMobile(mobile)) {
                showSnackBar(view, getString(R.string.hint_input_correct_mobile), SnackBarUtil.TYPE_WARNING);
                return;
            }
            if (mDataBinding.edtVerifyCode.length() == 0) {
                showSnackBar(view, getString(R.string.hint_input_verify_code), SnackBarUtil.TYPE_ERROR);
                return;
            }
            verifyCode = mDataBinding.edtVerifyCode.getText().toString().trim();
            mPresenter.verifyMobile(mobile, verifyCode);
            mDataBinding.btnComplete.setEnabled(false);
        }
    }


    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        if (observable instanceof CountDownEvent) {
            long leftTime = (long) data;
            if (leftTime > 0) {
                String text = String.format(getString(R.string.format_resend),leftTime / 1000);
                mDataBinding.btnVerifyCode.setText(text);
            } else {
                mDataBinding.btnVerifyCode.setEnabled(true);
                mDataBinding.btnVerifyCode.setText(R.string.get_verify_code);
            }
        }
    }

    @Override
    protected void onDestroy() {
        CountDownEvent.getInstance().destroy();
        super.onDestroy();
    }

    @Override
    public void onSendVerifyCodeCallback(boolean isSuccess) {
        if (isSuccess){
            showToast(getString(R.string.send_verify_code_success));
            CountDownEvent.getInstance().start(60 * 1000);
        } else {
            mDataBinding.btnVerifyCode.setEnabled(true);
        }

    }

    @Override
    public void onVerifyMobileCallback(boolean isSuccess) {
        if (isSuccess) {
            showToast(getString(R.string.verify_mobile_success));
            UserInformation.getInstance().user_info.BindCellphone = 1;
            UserInformation.save();
            MainActivity.start(VerifyMobileActivity.this);
            finishDelay();
        } else {
            mDataBinding.btnComplete.setEnabled(true);
        }
    }
}
